﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Diagnostics;

namespace WindowsFormsApplication3.基础信息管理
{
    public partial class baseInfoManaging : Form
    {
        public baseInfoManaging()
        {
            InitializeComponent();
        }

        //填充用户权限管理界面
        private void Read_DB_Fill_Grid(string table_name, DataGridView datagriview1)
        {
            // 用数据库将表格填满
            try
            {
                // 不自动增加dataGridview1 checkbox 增加行
                this.dataGridView1.AllowUserToAddRows = false;

                DataSet ds = 权限管理.permissionManaging.mySql.sql_search_database("select materialID,materialName,time,location from " + table_name);
                
                int colnum = ds.Tables[0].Columns.Count;         // 列数
                dataGridView1.ColumnCount = colnum;              // 设置列数
                int rownum = ds.Tables[0].Rows.Count;            // 行数
                dataGridView1.RowCount = rownum;                 // 设置行数
                for (int i = 0; i < rownum; i++)
                {
                    for (int j = 0; j < colnum; j++)
                    {
                        dataGridView1[j, i].Value = ds.Tables[0].Rows[i][j].ToString();     // 把那一行显示出来
                    }
                }
            }
            catch
            {
                for (int i = 0; i < dataGridView1.RowCount; i++)
                {
                    for (int j = 0; j < dataGridView1.ColumnCount; j++)
                    {
                        dataGridView1[j, i].Value = "";     // 把那一行显示出来
                    }
                }
            }
        }

        private void baseInfoManaging_Load(object sender, EventArgs e)
        {
            权限管理.permissionManaging.mySql.excute_sql("CREATE table baseInfotable(materialID int PRIMARY KEY,materialName nvarchar(50) ,time nvarchar(50),location nvarchar(50))"); //货物基础信息表
            Read_DB_Fill_Grid("baseInfotable", dataGridView1);
        }

        private void buttonAdd_Click(object sender, EventArgs e)
        {
            try
            {
                基础信息管理.material_add myMaterial_add = new 基础信息管理.material_add();
                myMaterial_add.ShowDialog();
                Read_DB_Fill_Grid("baseInfotable", dataGridView1);
            }
            catch
            {
                MessageBox.Show("添加货物失败");
            }
        }

        private void buttonEdit_Click(object sender, EventArgs e)
        {
            try
            {
                基础信息管理.baseInfo_edit myBaseInfo_edit = new 基础信息管理.baseInfo_edit();
                int selected_index = dataGridView1.SelectedCells[0].RowIndex;
                myBaseInfo_edit.temp_ID       = dataGridView1[0, selected_index].Value.ToString(); //货物ID
                myBaseInfo_edit.temp_name     = dataGridView1[1, selected_index].Value.ToString(); //货物名称
                myBaseInfo_edit.temp_time     = dataGridView1[2, selected_index].Value.ToString(); //货物入库时间
                myBaseInfo_edit.temp_location = dataGridView1[3, selected_index].Value.ToString(); //入库位置              
                权限管理.permissionManaging.mySql.excute_sql("delete from baseInfotable where materialID = '"
                    + myBaseInfo_edit.temp_ID + "'");             
                myBaseInfo_edit.ShowDialog();
                Read_DB_Fill_Grid("baseInfotable", dataGridView1);
            }
            catch
            {
                MessageBox.Show("更改信息失败");
            }
        }

        private void buttonDel_Click(object sender, EventArgs e)
        {
            try
            {
                基础信息管理.baseInfo_del myBaseInfo_del =new  基础信息管理.baseInfo_del();
                int selected_index = dataGridView1.SelectedCells[0].RowIndex;
                myBaseInfo_del.temp_delID = dataGridView1[0, selected_index].Value.ToString(); //货物ID
                myBaseInfo_del.ShowDialog();
                Read_DB_Fill_Grid("baseInfotable", dataGridView1);
            }
            catch
            {
                MessageBox.Show("删除信息失败");
            }
        }
    }
}
